using System.Collections;
using UnityEngine;
using System;
public enum WeaponType {
	Missile,
	Bullet
}

public enum FlameType {
	Blue,
	Red
}

public class ShipControler : MonoBehaviour {

	#region variables
	private WeaponType weaponType=WeaponType.Bullet;
	private FlameType flameColorType=FlameType.Blue;
	private IWeapon iWeapon;
	private IFlame iFlame;
	#endregion	
	private int seed;
	private void HandleWeaponType () {
		var weponTypeEnumLength=System.Enum.GetNames(weaponType.GetType()).Length;
		var currentWeaponType=(WeaponType)((seed++)%weponTypeEnumLength);

		switch (currentWeaponType) {
			case WeaponType.Bullet:
				iWeapon =new Bullet ()  ;
				break;
			case WeaponType.Missile:
				iWeapon = new Missile ();
				break;
			default:
				iWeapon = new Bullet () ;
				break;
			
		}
	}

	public void HandleFlameColor () {
		var flameColorEnumLength=System.Enum.GetNames(flameColorType.GetType()).Length;
		var currentFlameColor=(FlameType)((seed++)%flameColorEnumLength);

		Component c=this.GetComponent<IFlame>() as Component;
		if (c!=null){
			Destroy(c);
			iFlame.DestroyFlame();
		}
		#region Strategy
		switch (currentFlameColor) {

			case FlameType.Blue:
				iFlame = gameObject.AddComponent<BlueFlame> ();
				break;

			case FlameType.Red:
				iFlame = gameObject.AddComponent<RedFlame> ();
				break;

			default:
				iFlame = gameObject.AddComponent<BlueFlame> ();
				break;
		}
		#endregion

	}

	public void Fire () {
		iWeapon.Shoot (this.transform.position);
		
	}
	
	void Start () {

		HandleWeaponType (); //to check the value of weaponType in the inspector initially
		HandleFlameColor ();
		iFlame.ShowFlame ();
	}

	void Update () {

		if (Input.GetKeyDown (KeyCode.Space)) {
			//Debug.Log(this.weaponType);
			Fire ();
		}

		//to check the value of weaponType in the inspector while in play mode
		if (Input.GetKeyDown (KeyCode.C)) {
			//Debug.Log(this.weaponType);
			HandleWeaponType ();
		}

		if (Input.GetKeyDown (KeyCode.F)) {
			HandleFlameColor ();
			iFlame.ShowFlame ();
		}
	}
}